//



//  MBSearchResultVC.m
//  seachConstrollerTest
//
//  Created by MB__Lee on 2018/4/14.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "MBSearchResultVC.h"

@interface MBSearchResultVC ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableview;

@property(nonatomic,strong) NSMutableArray *dataSource;

@end

@implementation MBSearchResultVC
-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return  _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
}
-(void)setArrayFromVC:(NSMutableArray *)arrayFromVC{
    _arrayFromVC = arrayFromVC;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark ---searchResultUpdating
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
//    NSPredicate *predict = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c]%@",searchController.searchBar.text];
    
    
    //下面代码的意思：表示从数组里面用 predict 取出，包含字母“a” 的所有元素
    NSPredicate *predict = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@",@"a"];
    NSLog(@"========%@",searchController.searchBar.text);
        NSArray *filterArray =  [self.arrayFromVC filteredArrayUsingPredicate:predict];
    if (self.dataSource.count != 0) {
        [self.dataSource removeAllObjects];
    }
    [self.dataSource addObjectsFromArray:filterArray];
    [self.tableview reloadData];
    
    
    
}


#pragma mark --tableviewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return  1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identi = @"cell1";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identi];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identi];
    }
    cell.textLabel.text = self.dataSource[indexPath.row];
    return  cell;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
